#!/usr/bin/env bash

_preview() {
    res=$1
    args=(${res//:/ }) 
    # echo "tmux attach -t ${args[0]}"
    echo "tmux attach -t ${args[0]} && tmux select-window -t ${args[1]} && tmux select-pane -t ${args[2]}"
}

_cmdstr() {
    res=$1
    args=(${res//:/ }) 
    # echo "tmux attach -t ${args[0]}"
    echo "tmux attach -t ${args[0]} && tmux select-window -t ${args[1]} && tmux select-pane -t ${args[2]}"
}

_copytext() {
    res=$1
    args=(${res//:/ }) 
    # echo "tmux attach -t ${args[0]}"
    echo "tmux attach -t ${args[0]} && tmux select-window -t ${args[1]} && tmux select-pane -t ${args[2]}"
}

_fzftmux(){
# 判断是否存在 tmux
# tmuxsessions=$(tmux list-sessions -F "#{session_name}")
tmuxsessions=$(tmux ls)
# echo $? 
# 如果$?等于1表示上条命令执行有问题，一般指令程序倘若执行成功，$?其回传值为 0；失败为 1
# -eq 等于
# -ne 不等于
# -gt 大于
# -lt 小于
# -ge 大于等于
# -le 小于等于
if [ $? -eq 1 ]; then
    # echo "[Debug]传值为 1，指令程序倘若执行失败。"
    # echo "[Debug]不存在正在运行的tmux session"
    tmux  # 创建tmux
else
    # echo "[Debug]传值不为 1，指令程序倘若执行成功。"
    # echo "[Debug]存在正在运行的tmux session"
    # 判断当前命令是在tmux窗口中执行还是非tmux窗口
    # [ -z "${TMUX}" ] && echo "not in tmux"
    if [ -z "${TMUX}" ]; then
        # echo "[Debug] not in tmux"
        # customizable
        LIST_DATA="#{window_name} #{pane_title} #{pane_current_path} #{pane_current_command}"
        # do not change
        TARGET_SPEC="#{session_name}:#{window_id}:#{pane_id}:"
        LINE=$(tmux list-panes -a -F "$TARGET_SPEC $LIST_DATA")
        # echo "[Debug] LINE = "$LINE;
        # 选择操作
        option=$(echo -e "$LINE" | fzf  \
                    --cycle \
                    --header='请选择(tab复制)' \
                    --header-first \
                    --bind='tab:execute(h=$(~/.config/scripts/fzf-switch-or-create-tmux.sh cmdstr {}) && echo $h | pbcopy)+abort' \
                    --preview="~/.config/scripts/fzf-switch-or-create-tmux.sh preview {}" \
                    --height=81% \
                    --preview-window=bottom:1:wrap
        )
        # echo "[Debug] option = "$option
        args=(${option//:/ })  # 结果切割
        # tmux select-pane -t ${args[2]} && tmux select-window -t ${args[1]} && tmux switch-client -t ${args[0]}
        
        # # 复制到剪贴板
        # # echo -e "copy $(_copytext "$server") ..."
        # # echo -e "copy passwd  ..."
        # echo -e "copy "$(_copytext "$option")" ..."
        # echo -e "$(_copytext "$option")" | pbcopy  
        # echo -e "copy success."

        # 执行命令
        tmux attach -t ${args[0]} && tmux select-window -t ${args[1]} && tmux select-pane -t ${args[2]}
    else
        # echo "[Debug]in tmux"
        tmux choose-tree     # 打开 tmux 窗口树
    fi
fi

# echo "[Debug]"$tmuxsessions
}

case $1 in
    "") _fzftmux ;;
    preview) _preview "$2" ;;
    copytext) _copytext "$2" ;;
    cmdstr) _cmdstr "$2" ;;
    *) _fzftmux $* ;;
esac

# 参考资料
# [tmux and fzf: fuzzy tmux session/window/pane switcher – Eioki's Public Place](https://eioki.eu/2021/01/12/tmux-and-fzf-fuzzy-tmux-session-window-pane-switcher)
# [如何通过bash脚本判断我是否处于tmux会话中？](https://qastack.cn/unix/10689/how-can-i-tell-if-im-in-a-tmux-session-from-a-bash-script)
